package cn.jtfadmin.base.dict.config.excel;

import cn.afterturn.easypoi.handler.inter.IExcelDictHandler;
import cn.jtfadmin.base.dict.dto.DictItemModelDTO;
import cn.jtfadmin.base.dict.service.DictService;
import cn.jtfadmin.base.lang.config.ApplicationHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * easypoi 字典处理类型<br/>
 * 使用位置：excel 导入导出，牵涉到字典的<br/>
 *
 * @author jtf
 * @see cn.afterturn.easypoi.excel.entity.ImportParams#setDictHandler(IExcelDictHandler)
 * @see cn.afterturn.easypoi.excel.entity.ExportParams#setDictHandler(IExcelDictHandler)
 * @since 0.0.1
 */
@Component
public class ExcelDictHandler implements IExcelDictHandler {

    @Autowired
    protected DictService dictService;

    @Override
    public String toName(String dict, Object obj, String name, Object value) {

        String dictText = dictService.getDictText(dict, String.valueOf(value));
        return dictText;
    }

    @Override
    public String toValue(String dict, Object obj, String name, Object value) {
        DictService bean = dictService;
        List<DictItemModelDTO> dictItemModelByCode = bean.findDictItemModelByCode(dict);
        for (DictItemModelDTO sysDictModelRDTO : dictItemModelByCode) {
            if (Objects.equals(value, sysDictModelRDTO.getText())) {
                return sysDictModelRDTO.getValue();
            }
        }
        return null;
    }
}
